# -*- coding: utf-8 -*-

import arcpy

topdldm = """
def topdldm(aa):
    if aa[0] != "2":
        cc = aa[0]
    else:
        cc = aa
    return cc"""

disdldm = """
def disdldm(JOIN_DLDM, JOIN_DIS , DisNO, DLDM):
    i = 1
    CATCH = 0
    for j in JOIN_DLDM.split(',')[1:]:
        if DLDM == j:
            DisNO = JOIN_DIS.split(',')[i]
            CATCH = 1
            break
        i += 1
    if CATCH == 0:
        i = 1
        for j in JOIN_DLDM.split(',')[1:]:
            if ( (DLDM[0] == j[0]) and (DLDM[0] != '2')):
                DisNO = JOIN_DIS.split(',')[i]
                CATCH = 1
                break
            i += 1  

    return DisNO"""

ori = "ORI"
singlepart = "SINGLEPART"
more_then_400 = "more_then_400"
less_then_400 = "less_then_400"
dissolve_less_than_400 = "dissolve_less_than_400"
dissolve_less_than_400_2st = "dissolve_less_than_400_2st"
dissolve_less_than_400_2st_spatial_join = "dissolve_less_than_400_2st_spatial_join"
target = "final"
ALL_MERGE = "all_merge"
result = 'result'
arcpy.env.workspace = r"e:\HEBING\HEBING.MDB"
arcpy.env.overwriteOutput = True
list_Feature = arcpy.ListFeatureClasses()
for each in list_Feature:
    if each != "ORI":
        arcpy.Delete_management(each)
        print("删除图层%s".decode("utf-8") % each)
# 将ORI原始图层的多部件要素转为单部件要素
arcpy.MultipartToSinglepart_management(ori, singlepart)
# 为SINGLEPART图层添加唯一值字段DisNO
print("添加唯一值字段".decode("utf-8"))
arcpy.AddField_management(singlepart, "DisNO", "TEXT", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")
# 为SINGLEPART图层添加面积字段MJ
print("添加面积字段".decode("utf-8"))
arcpy.AddField_management(singlepart, "MJ", "DOUBLE", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")
# 为SINGLEPART图层计算唯一值
print("计算唯一值".decode("utf-8"))
arcpy.CalculateField_management(singlepart, "DisNO", "!OBJECTID!", "PYTHON_9.3")
# 为SINGLEPART图层计算面积
print("计算面积".decode("utf-8"))
arcpy.CalculateField_management(singlepart, "MJ", "!shape.area!", "PYTHON_9.3")
# 导出SINGLEPART图层中面积小于400的面到less_then_400图层
print("导出面积小于400的面".decode("utf-8"))
arcpy.Select_analysis(singlepart, less_then_400, 'MJ <  400')
# 导出SINGLEPART图层中面积大于等于400的面到more_then_400
print("导出面积大于400的面".decode("utf-8"))
arcpy.Select_analysis(singlepart, more_then_400, 'MJ >=  400')
# 融合less_then_400图层中坐落代码一致且地类代码一致的相邻要素，输出为dissolve_less_than_400,输出字段ZLDWDM,GHDLDM,GHDLMC,DisNO
print("融合面积小于400面中坐落代码一致且地类代码一致的相邻要素".decode("utf-8"))
arcpy.Dissolve_management(less_then_400, dissolve_less_than_400, "GHDLDM", "GHDLMC LAST;DisNO LAST",
                          "SINGLE_PART",
                          "DISSOLVE_LINES")
# 修改dissolve_less_than_400图层字段LAST_GHDLMC为GHDLMC
print("修改字段名为GHDLMC".decode("utf-8"))
arcpy.AlterField_management(dissolve_less_than_400, "LAST_GHDLMC", "GHDLMC", "", "", "", "NON_NULLABLE", "false")
# 修改dissolve_less_than_400图层字段LAST_DisNO为DisNO
print("修改字段名为DisNO".decode("utf-8"))
arcpy.AlterField_management(dissolve_less_than_400, "LAST_DisNO", "DisNO", "", "", "", "NON_NULLABLE", "false")
# 为dissolve_less_than_400图层添加面积字段MJ
print("添加面积字段".decode("utf-8"))
arcpy.AddField_management(dissolve_less_than_400, "MJ", "DOUBLE", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")
# 计算面积
print("计算面积".decode("utf-8"))
arcpy.CalculateField_management(dissolve_less_than_400, "MJ", "!shape.area!", "PYTHON_9.3")
# 添加一级地类字段TOPDL
print("添加一级地类字段".decode("utf-8"))
arcpy.AddField_management(dissolve_less_than_400, "TOPDL", "TEXT", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")
# 计算一级地类
print("计算一级地类".decode("utf-8"))
arcpy.CalculateField_management(dissolve_less_than_400, "TOPDL", "topdldm( !GHDLDM! )", "PYTHON_9.3",
                                topdldm)
# 融合一级地类一致的面
print("融合一级地类一致的面".decode("utf-8"))
arcpy.Dissolve_management(dissolve_less_than_400, dissolve_less_than_400_2st, "ZLDWDM;TOPDL",
                          "GHDLMC FIRST;DisNO FIRST;GHDLDM FIRST", "SINGLE_PART", "DISSOLVE_LINES")
#
# # 为SINGLEPART图层计算唯一值
# print ("计算唯一值".decode("utf-8") )
# arcpy.CalculateField_management(dissolve_less_than_400_2st, "FIRST_DisNO", "int(!OBJECTID!)+120000", "PYTHON_9.3")
print("小面与大面空间连接".decode("utf-8"))

arcpy.SpatialJoin_analysis(dissolve_less_than_400_2st, more_then_400, dissolve_less_than_400_2st_spatial_join,
                           "JOIN_ONE_TO_ONE", "KEEP_ALL",
                           "ZLDWDM \"ZLDWDM\" true true false 19 Text 0 0 ,First,#,dissolve_less_than_400_2st,ZLDWDM,-1,-1;DisNO \"DisNO\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st,FIRST_DisNO,-1,-1;GHDLDM \"GHDLDM\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st,FIRST_GHDLDM,-1,-1;TOPDL \"TOPDL\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st,TOPDL,-1,-1;GHDLMC \"GHDLMC\" true true false 50 Text 0 0 ,First,#,dissolve_less_than_400_2st,FIRST_GHDLMC,-1,-1;JOIN_DLDM \"JOIN_DLDM\" true true false 255 Text 0 0 ,Join,\",\",dissolve_less_than_400_2st,FIRST_GHDLDM,-1,-1,more_then_400,GHDLDM,-1,-1;JOIN_DIS \"JOIN_DIS\" true true false 255 Text 0 0 ,Join,\",\",dissolve_less_than_400_2st,FIRST_DisNO,-1,-1,more_then_400,DisNO,-1,-1",
                           "INTERSECT", "", "")
print("计算空间连接值".decode("utf-8"))
# Process: Calculate Field
arcpy.CalculateField_management(dissolve_less_than_400_2st_spatial_join, "DisNO",
                                "disdldm( !JOIN_DLDM! , !JOIN_DIS! , !DisNO!, !GHDLDM! )", "PYTHON_9.3",
                                disdldm)
##  Process: 合并
print("合并全部面".decode("utf-8"))
arcpy.Merge_management("dissolve_less_than_400_2st_spatial_join;more_then_400", ALL_MERGE,
                       "Join_Count \"Join_Count\" true true false 4 Long 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,Join_Count,-1,-1;TARGET_FID \"TARGET_FID\" true true false 4 Long 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,TARGET_FID,-1,-1;ZLDWDM \"ZLDWDM\" true true false 19 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,ZLDWDM,-1,-1,more_then_400,ZLDWDM,-1,-1;DisNO \"DisNO\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,DisNO,-1,-1,more_then_400,DisNO,-1,-1;GHDLDM \"GHDLDM\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,GHDLDM,-1,-1,more_then_400,GHDLDM,-1,-1;TOPDL \"TOPDL\" true true false 20 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,TOPDL,-1,-1;GHDLMC \"GHDLMC\" true true false 50 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,GHDLMC,-1,-1,more_then_400,GHDLMC,-1,-1;JOIN_DLDM \"JOIN_DLDM\" true true false 255 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,JOIN_DLDM,-1,-1;JOIN_DIS \"JOIN_DIS\" true true false 255 Text 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,JOIN_DIS,-1,-1;Shape_Length \"Shape_Length\" false true true 8 Double 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,Shape_Length,-1,-1,more_then_400,Shape_Length,-1,-1;Shape_Area \"Shape_Area\" false true true 8 Double 0 0 ,First,#,dissolve_less_than_400_2st_spatial_join,Shape_Area,-1,-1,more_then_400,Shape_Area,-1,-1")

# Process: 融合
print("融合小图斑".decode("utf-8"))
arcpy.Dissolve_management(ALL_MERGE, result, "ZLDWDM;DisNO", "GHDLDM FIRST;GHDLMC FIRST", "SINGLE_PART",
                          "DISSOLVE_LINES")
#
#
#
# # 为result图层添加面积字段MJ
# print ("添加长度面积比字段".decode("utf-8") )
# arcpy.AddField_management(result, "长度面积比", "DOUBLE", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "")

# 为SINGLEPART图层计算面积
# print ("计算长度面积比".decode("utf-8") )
# arcpy.CalculateField_management(result, "长度面积比", "!shape.length@meter!/!shape.area!", "PYTHON_9.3")